//
//  GroupRootViewController.m
//  Cycling
//
//  Created by cjg.mini on 2023/3/2.
//

#import "GroupRootViewController.h"
#import "WebViewJavascriptBridge.h"
#import "BaseRequestApi.h"
#import <CoreLocation/CoreLocation.h>

@interface GroupRootViewController ()<WKNavigationDelegate,CLLocationManagerDelegate>

@property WebViewJavascriptBridge* bridge;
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocation *myLocation;
@property (nonatomic, strong) WKWebView* webView;
@end

@implementation GroupRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self startLocation];

}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (_bridge) { return; }
    [self.navigationController setNavigationBarHidden:YES animated:NO];

    WKWebView* webView = [[NSClassFromString(@"WKWebView") alloc] initWithFrame:CGRectZero];
    [webView setOpaque:NO];
    webView.navigationDelegate = self;
    webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self loadExamplePage:webView];

    [self.view addSubview:webView];
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(@0);
        make.bottom.equalTo(self.view).offset(-60-SafeAreaBottomHeight);
    }];
    self.webView = webView;
    
    [WebViewJavascriptBridge enableLogging];
    _bridge = [WebViewJavascriptBridge bridgeForWebView:webView];
    [_bridge setWebViewDelegate:self];

    [_bridge registerHandler:@"loadUserInfo" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"testObjcCallback called: %@", data);
        NSString *token = [USERDEFAULTS valueForKey:@"Token"];
        NSString *language = [[CycLanguage sharedInstance] currentLanguage];
        responseCallback(@{@"token":token,@"lang":language});
    }];
    [_bridge registerHandler:@"getTopHeight" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"testObjcCallback called: %@", data);
        NSString *height = [NSString stringWithFormat:@"%f",SafeAreaTopHeight];
        responseCallback(@{@"height":height});
    }];
    
    [_bridge registerHandler:@"onBackPress" handler:^(id data, WVJBResponseCallback responseCallback) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    // 设置经纬度
    [_bridge registerHandler:@"location" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"testObjcCallback called: %@", data);
        NSString * lat = [NSString stringWithFormat:@"%f",self.myLocation.coordinate.latitude];
        NSString * lng = [NSString stringWithFormat:@"%f",self.myLocation.coordinate.longitude];

        responseCallback(@{@"lat":lat,@"lng":lng});
    }];

}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    NSLog(@"webViewDidStartLoad");
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSLog(@"webViewDidFinishLoad");
}

- (void)loadExamplePage:(WKWebView*)webView {
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:Cyc_WEBURL_GROUP]]];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];

}

//开始定位
- (void)startLocation {
    //初始化定位管理者
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    //判断设备是否能够进行定位
    if ([CLLocationManager locationServicesEnabled]) {
        //精确度获取到米
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        //设置过滤器为无
        self.locationManager.distanceFilter = kCLDistanceFilterNone;
        // 取得定位权限，有两个方法，取决于你的定位使用情况
        //一个是requestAlwaysAuthorization，一个是requestWhenInUseAuthorization
        [self.locationManager requestWhenInUseAuthorization];
        //开始获取定位
        [self.locationManager startUpdatingLocation];
        //地理信息
//        self.geoCoder = [[CLGeocoder alloc] init];
    } else {
        DebugLog(@"error");
    }
}

//设置获取位置信息的代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    NSLog(@"%lu", (unsigned long)locations.count);
    self.myLocation = locations.lastObject;
    DebugLog(@"经度：%f 纬度：%f", _myLocation.coordinate.longitude, _myLocation.coordinate.latitude);
    
    //不用的时候关闭更新位置服务，不关闭的话这个 delegate 隔一定的时间间隔就会有回调
    [self.locationManager stopUpdatingLocation];
    [self loadExamplePage:self.webView];

}
//问题函数
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    if (error) {
        DebugLog(@"error:%ld", (long)error.code);
    }
}


@end
